﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace Dao
{
    public class JCCDMXB_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(JCCDMXB entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.JCCDMXBSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(JCCDMXB entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                JCCDMXB entity2 = context.JCCDMXBSet.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2 = entity;
                    context.JCCDMXBSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                JCCDMXB entity = context.JCCDMXBSet.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public JCCDMXB Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                JCCDMXB entity = context.JCCDMXBSet.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<JCCDMXB> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.JCCDMXBSet
                        select c;
                List<JCCDMXB> list = new List<JCCDMXB>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }

        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<JCCDMXB> QueryBySql(string sql)
        {
            List<JCCDMXB> list = new List<JCCDMXB>();
            using (var context = new Dao.ErpEntities())
            {
                ObjectQuery<JCCDMXB> q = context.CreateQuery<JCCDMXB>(sql);
                ObjectResult<JCCDMXB> results = q.Execute(MergeOption.NoTracking);
                foreach (JCCDMXB c in results)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public List<JCCDMXB> QueryByJCCDID(string jccdid)
        {
            List<JCCDMXB> list = new List<JCCDMXB>();
            using (var context = new Dao.ErpEntities())
            {
                Guid jid = Guid.Parse(jccdid);
                var q = from j in context.JCCDMXBSet
                        where j.JCCDID == jid
                        select j;

                foreach (JCCDMXB c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public List<vJCCDMXB> QueryByJCCDIDEx(string jccdid)
        {
            List<vJCCDMXB> list = new List<vJCCDMXB>();
            using (var context = new Dao.ErpEntities())
            {
                Guid jid = Guid.Parse(jccdid);
                var q = from j in context.vJCCDMXBSet
                        where j.JCCDID == jid
                        select j;

                foreach (vJCCDMXB c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

    }
}
